package test.administrator.example.com.mvptest1.framework.lce.impl.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.XRefreshViewFooter;
import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;

import test.administrator.example.com.mvptest1.R;
import test.administrator.example.com.mvptest1.framework.support.base.MvpBasePresenter;
import test.administrator.example.com.mvptest1.framework.MvpView;
import test.administrator.example.com.mvptest1.list.base.decoration.DividerItemDecoration;


/**
 * 这个类中代码你随时随刻随意改变都可以，只要满足你的需求即可（动态改变）
 */
public abstract class BaseRefreshFragment<V extends MvpView, P extends MvpBasePresenter<V>> extends BaseMvpFragment<V, P> {

	private XRefreshView refreshView;
	private RecyclerView recyclerView;
	private BaseRecyclerAdapter recyclerAdapter;
	private LinearLayoutManager linearLayoutManager;

	public XRefreshView getRefreshView() {
		return refreshView;
	}

	public BaseRecyclerAdapter getAdapter() {
		return recyclerAdapter;
	}

	public LinearLayoutManager getLinearLayoutManager() {
		return linearLayoutManager;
	}

	private boolean isDownRefresh;

	public boolean isDownRefresh() {
		return isDownRefresh;
	}

	@Override
	public void initContentView(View contentView) {
		initRefreshView(contentView);
	}

	/**
	 * 初始化下拉刷新组件
	 *
	 * @param contentView
	 */
	public void initRefreshView(View contentView) {
		//注意：以下代码可以根据自己开发中使用的下拉刷新组件进行替换
		//下拉刷新组件设置
		refreshView = (XRefreshView) contentView.findViewById(R.id.xrefreshview);
		//是否可以下拉刷新,true代表可以,false代表不支持
		refreshView.setPullRefreshEnable(true);
		//是否允许加载更多
		refreshView.setPullLoadEnable(true);
		//设置下拉刷新完成之后,刷新头部停留的时间
		refreshView.setPinnedTime(1000);
		//设置是否自动刷新(进入页面自动刷新)
		refreshView.setAutoRefresh(false);


		//初始化列表(也可以替换)
		recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerview);
		recyclerView.setHasFixedSize(true);
		linearLayoutManager = new LinearLayoutManager(getContext());
		//设置列表为垂直方向显示
		recyclerView.setLayoutManager(linearLayoutManager);
		//添加分割线
		recyclerView.addItemDecoration(new DividerItemDecoration(20));

		//绑定Adapter
		recyclerAdapter = bindAdapter();
		recyclerView.setAdapter(recyclerAdapter);

		//给我们的Adapter添加加载更多的布局
		recyclerAdapter.setCustomLoadMoreView(new XRefreshViewFooter(getContext()));

		//添加下拉刷新监听
		refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

			/**
			 * 下拉刷新
			 */
			@Override
			public void onRefresh() {
				super.onRefresh();
				refreshData(true);
			}

			/**
			 * 上拉加载更多
			 * @param isSlience
			 */
			@Override
			public void onLoadMore(boolean isSlience) {
				super.onLoadMore(isSlience);
				refreshData(false);
			}
		});
	}

	public abstract BaseRecyclerAdapter bindAdapter();

	/**
	 * 提供子类实现这个刷新方法(调用具体网络请求代码)
	 */
	public void refreshData(boolean isDownRefresh) {
		this.isDownRefresh = isDownRefresh;
	}

}
